﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.IO;
using System.Net;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using System.Xml;
using System.ComponentModel;//BackgroundWorker所在的
using System.Collections.Generic;
using FBWMS.Properties;

namespace FBWMS
{
    public class G_CommonClassSystem
    {
        //コンストラクタ
        public G_CommonClassSystem()
        {
        }

        ////////////////////////////////////////////////////////////////////////////
        // OutLine   : G_System_GetConnectionString
        // Parameter : なし
        // Return    : 接続文字列 Or 空白
        // Contents  : DB接続文字列生成
        // Makes     : 渡辺
        // History   : 2008/03/04       : 初版
        //           :
        ////////////////////////////////////////////////////////////////////////////
        public static string G_System_GetConnectionString()
        {
            string str_ConnectionString = "";
            str_ConnectionString = str_ConnectionString + "Data Source=";
            str_ConnectionString = str_ConnectionString + G_CommonDefine.G_DBServer.ToString();
            str_ConnectionString = str_ConnectionString + ";Initial Catalog=";
            str_ConnectionString = str_ConnectionString + G_CommonDefine.G_DBCatalog.ToString();
            str_ConnectionString = str_ConnectionString + ";Persist Security Info=";
            str_ConnectionString = str_ConnectionString + G_CommonDefine.G_DBSecurity.ToString();
            str_ConnectionString = str_ConnectionString + ";User ID=";
            str_ConnectionString = str_ConnectionString + G_CommonDefine.G_DBUser.ToString();
            str_ConnectionString = str_ConnectionString + ";Password=";
            str_ConnectionString = str_ConnectionString + G_CommonDefine.G_DBPass.ToString();

            //接続文字列を返す
            return str_ConnectionString;
        }

        public static string P_OpenFileDialog(string str_Title, string str_InitialDir, string str_InitialFile, string str_Filter)
        {
            // openFileDialog の新しいインスタンスを生成する
            OpenFileDialog openFileDialog1 = new OpenFileDialog();

            // ダイアログのタイトルを設定する
            openFileDialog1.Title = str_Title;

            // 初期表示するディレクトリを設定する
            openFileDialog1.InitialDirectory = str_InitialDir;

            // 初期表示するファイル名を設定する
            openFileDialog1.FileName = str_InitialFile;

            // ファイルのフィルタを設定する
            openFileDialog1.Filter = str_Filter;

            // [ヘルプ] ボタンを表示する (初期値 false)
            openFileDialog1.ShowHelp = true;

            // 存在しないファイル名を指定した場合は警告を表示する (初期値 false)
            openFileDialog1.CheckFileExists = true;

            // 存在しないパスを指定した場合は警告を表示する (初期値 true)
            openFileDialog1.CheckPathExists = true;

            // 拡張子を指定しない場合は自動的に拡張子を付加する (初期値 true)
            openFileDialog1.AddExtension = true;

            // ダイアログを表示し、戻り値が [OK] の場合は、選択したファイルを表示する
            string str_FilePath = "";

            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                str_FilePath = openFileDialog1.FileName;
            }

            // 不要になった時点で破棄する (正しくは オブジェクトの破棄を保証する を参照)
            openFileDialog1.Dispose();

            return str_FilePath;
        }

        ////////////////////////////////////////////////////////////////////////////
        // OutLine   : G_System_GetOSVersion
        // Parameter : なし
        // Return    : True Or False
        // Contents  : OS情報取得
        // Makes     : 渡辺
        // History   : 2007/06/14       : 初版
        //           :
        ////////////////////////////////////////////////////////////////////////////
        public static bool G_System_GetOSVersion()
        {
            try
            {
                //ログ書込み
                G_System_SetLog(G_CommonDefine.LogMode.ProcStart, "G_System_GETOSVersion処理開始", "G_System_GETOSVersion");

                // OSの情報を取得
                OperatingSystem osInfo = Environment.OSVersion;

                string windowsName = Resources.不明;  // Windows名

                switch (osInfo.Platform)
                {
                    case PlatformID.Win32Windows:  // Windows 9x系
                        if (osInfo.Version.Major == 4)
                        {
                            switch (osInfo.Version.Minor)
                            {
                                case 0:  // .NET Frameworkのサポートなし
                                    windowsName = "Windows 95";
                                    break;
                                case 10:
                                    windowsName = "Windows 98";
                                    break;
                                case 90:
                                    windowsName = "Windows Me";
                                    break;
                            }
                        }
                        break;

                    case PlatformID.Win32NT:  // Windows NT系
                        if (osInfo.Version.Major == 4)
                        {
                            // .NET Framework 2.0以降のサポートなし
                            windowsName = "Windows NT 4.0";
                        }
                        else if (osInfo.Version.Major == 5)
                        {
                            switch (osInfo.Version.Minor)
                            {
                                case 0:
                                    windowsName = "Windows 2000";
                                    break;

                                case 1:
                                    windowsName = "Windows XP";
                                    break;

                                case 2:
                                    windowsName = "Windows Server 2003";
                                    break;
                            }
                        }
                        else if (osInfo.Version.Major == 6)
                        {
                            switch (osInfo.Version.Minor)
                            {
                                case 0:
                                    windowsName = "Windows Vista";
                                    break;
                            }
                        }
                        break;
                }

                // システム情報を出力
                /*
                                Console.WriteLine(
                                  "{0} (Platform {1} Version {2}.{3} Build {4}) {5}",
                                  windowsName,
                                  osInfo.Platform,
                                  osInfo.Version.Major,
                                  osInfo.Version.Minor,
                                  osInfo.Version.Build,
                                  osInfo.ServicePack);  // サービス・パック名
                */
                G_CommonDefine.G_str_WindowsName = windowsName;
                G_CommonDefine.G_os_osInfo = osInfo;

                //ログ書込み
                G_System_SetLog(G_CommonDefine.LogMode.ProcEnd, Resources.GSystemGETOSVersion処理終了, "G_System_GETOSVersion");

                //正常値を返す
                return true;
            }
            catch (Exception err)
            {
                //ログ書込み
                G_System_SetLog(G_CommonDefine.LogMode.Error, Resources.G_System_GetOSVersion + err.Message, "G_System_GETOSVersion");

                //エラーメッセージ表示
                MessageBox.Show(Resources.PGETSystemInfo処理エラー + err.Message, Resources.エラー, MessageBoxButtons.OK, MessageBoxIcon.Error);

                //異常値を返す
                return false;
            }
        }

        ////////////////////////////////////////////////////////////////////////////
        // OutLine   : G_System_GetSystemInfo
        // Parameter : なし
        // Return    : True Or False
        // Contents  : M_SysManager情報取得
        // Makes     : 渡辺
        // History   : 2007/06/14       : 初版
        //           :
        ////////////////////////////////////////////////////////////////////////////
        public static bool G_System_GetSystemInfo()
        {
            try
            {
                //ログ書込み
                G_System_SetLog(G_CommonDefine.LogMode.ProcStart, Resources.PGETSystemInfo処理開始, "P_GET_SystemInfo");

                //データセット宣言
                DataSet ds;
                SqlConnection connect;

                //DB接続
                if (!G_CommonClassDB.G_DB_Connect(out connect))
                {
                    //エラーメッセージ表示
                    MessageBox.Show(Resources.P_Search);

                    return false;
                }

                SqlCommand sqlCmd = connect.CreateCommand();
                SqlParameter[] sqlPara = new SqlParameter[1];
                sqlPara[0] = G_CommonClassDB.G_DB_SetSQLDBParameter("@Tori_CD", ParameterDirection.Input, SqlDbType.VarChar, 20, "COM");

                //ストアド実行(帳票出力レコード取得)
                switch (G_CommonClassDB.G_DB_ExecStoredProc(connect, sqlCmd, "USP_COM_M_Sys_Manager_DataSelect", sqlPara, out ds))
                {
                    case G_CommonDefine.RetValue.GetData:

                        //帳票出力PC名称
                        G_CommonDefine.G_MSys_Printer_PC_NM = ds.Tables[0].Rows[0]["Printer_PC_NM"].ToString();
                        //仮引当PC名称
                        G_CommonDefine.G_MSys_Mortgage_Temporary_PC_NM = ds.Tables[0].Rows[0]["Mortgage_Temporary_PC_NM"].ToString();

                        //DB開放
                        if (!G_CommonClassDB.G_DB_Dispose(connect))
                        {
                            //エラーメッセージ表示
                            MessageBox.Show(Resources.UpdateData);
                            return false;
                        }

                        break;
                    default:
                        break;
                }

                //ログ書込み
                G_System_SetLog(G_CommonDefine.LogMode.ProcEnd, Resources.PGETSystemInfo処理終了, "P_GET_SystemInfo");

                //正常値を返す
                return true;
            }
            catch (Exception err)
            {
                //ログ書込み
                G_System_SetLog(G_CommonDefine.LogMode.Error, Resources.PGETSystemInfo処理エラー + err.Message, "P_GET_SystemInfo");

                //エラーメッセージ表示
                MessageBox.Show(Resources.PGETSystemInfo処理エラー + err.Message, Resources.エラー, MessageBoxButtons.OK, MessageBoxIcon.Error);

                //異常値を返す
                return false;
            }
        }

        ////////////////////////////////////////////////////////////////////////////
        // OutLine   : G_System_GetCommonDefine
        // Parameter : なし
        // Return    : True Or False
        // Contents  : CommonDefine.xml読込み用
        // Makes     : 渡辺
        // History   : 2007/06/14       : 初版
        //           :
        ////////////////////////////////////////////////////////////////////////////
        public static bool G_System_GetCommonDefine()
        {
            try
            {
                //ログ書込み
                //G_CommonClassSystem.G_System_SetLog(G_CommonDefine.LogMode.ProcStart, "P_GET_CommonDefine処理開始", "P_GET_CommonDefine");

                string str_CommonDefinePath = G_CommonDefine.G_CommonPath + G_CommonDefine.CommonFile.CommonDefine;

                //iniファイルの存在を確認して無ければダウンロード
                if (!File.Exists(str_CommonDefinePath))
                {
                    MessageBox.Show(Resources.Iniファイルをタウンロートします, Resources.確認, MessageBoxButtons.OK, MessageBoxIcon.Information);

                    //待機画面表示
                    G_CommonClassOther.G_Other_WaitForm(true);

                    string str_ServerXML = G_CommonDefine.G_SystemPC_exePathServer.ToString() + "CommonDefine.xml";
                    string str_LocalXML = G_CommonDefine.G_CommonPath.ToString() + "CommonDefine.xml";

                    File.Copy(str_ServerXML, str_LocalXML, true);

                    //待機画面解除
                    G_CommonClassOther.G_Other_WaitForm(false);
                }

                xmlData[,] getDBInfo;
                xmlData[,] getSystemPCInfo;
                xmlData[,] getEncryptInfo;
                xmlData[,] getEDIInfo;
                xmlData[,] getFMTInfo;
                xmlData[,] getStartUpInfo;
                xmlData[,] getLogInfo;
                xmlData[,] getColorInfo;
                xmlData[,] getToolTipInfo;
                xmlData[,] getIntervalInfo;

                string[] Nodes = new string[2];

                Nodes[0] = G_CommonDefine.NodeNM.Common.connectionNode;
                Nodes[1] = G_CommonDefine.NodeNM.Common.SubDB;

                switch (G_CommonClassXML.G_XML_Read("", G_CommonDefine.CommonFile.CommonDefine, Nodes, out getDBInfo))
                {
                    case G_CommonDefine.RetValue.FoldarNotExistsError:
                    case G_CommonDefine.RetValue.FileNotExistsError:
                    case G_CommonDefine.RetValue.UndefinedError:
                    case G_CommonDefine.RetValue.NoData:
                        return false;
                    case G_CommonDefine.RetValue.GetData:
                        //正常処理
                        break;
                    default:
                        return false;
                }

                Nodes[0] = G_CommonDefine.NodeNM.Common.connectionNode;
                Nodes[1] = G_CommonDefine.NodeNM.Common.SubSystemPC;

                switch (G_CommonClassXML.G_XML_Read("", G_CommonDefine.CommonFile.CommonDefine, Nodes, out getSystemPCInfo))
                {
                    case G_CommonDefine.RetValue.FoldarNotExistsError:
                    case G_CommonDefine.RetValue.FileNotExistsError:
                    case G_CommonDefine.RetValue.UndefinedError:
                    case G_CommonDefine.RetValue.NoData:
                        return false;
                    case G_CommonDefine.RetValue.GetData:
                        //正常処理
                        break;
                    default:
                        return false;
                }

                Nodes[0] = G_CommonDefine.NodeNM.Common.connectionNode;
                Nodes[1] = G_CommonDefine.NodeNM.Common.SubEncrypt;

                switch (G_CommonClassXML.G_XML_Read("", G_CommonDefine.CommonFile.CommonDefine, Nodes, out getEncryptInfo))
                {
                    case G_CommonDefine.RetValue.FoldarNotExistsError:
                    case G_CommonDefine.RetValue.FileNotExistsError:
                    case G_CommonDefine.RetValue.UndefinedError:
                    case G_CommonDefine.RetValue.NoData:
                        return false;
                    case G_CommonDefine.RetValue.GetData:
                        //正常処理
                        break;
                    default:
                        return false;
                }

                Nodes[0] = G_CommonDefine.NodeNM.Common.connectionNode;
                Nodes[1] = G_CommonDefine.NodeNM.Common.SubEDI;

                switch (G_CommonClassXML.G_XML_Read("", G_CommonDefine.CommonFile.CommonDefine, Nodes, out getEDIInfo))
                {
                    case G_CommonDefine.RetValue.FoldarNotExistsError:
                    case G_CommonDefine.RetValue.FileNotExistsError:
                    case G_CommonDefine.RetValue.UndefinedError:
                    case G_CommonDefine.RetValue.NoData:
                        return false;
                    case G_CommonDefine.RetValue.GetData:
                        //正常処理
                        break;
                    default:
                        return false;
                }

                Nodes[0] = G_CommonDefine.NodeNM.Common.connectionNode;
                Nodes[1] = G_CommonDefine.NodeNM.Common.SubFMT;

                switch (G_CommonClassXML.G_XML_Read("", G_CommonDefine.CommonFile.CommonDefine, Nodes, out getFMTInfo))
                {
                    case G_CommonDefine.RetValue.FoldarNotExistsError:
                    case G_CommonDefine.RetValue.FileNotExistsError:
                    case G_CommonDefine.RetValue.UndefinedError:
                    case G_CommonDefine.RetValue.NoData:
                        return false;
                    case G_CommonDefine.RetValue.GetData:
                        //正常処理
                        break;
                    default:
                        return false;
                }

                Nodes[0] = G_CommonDefine.NodeNM.Common.connectionNode;
                Nodes[1] = G_CommonDefine.NodeNM.Common.SubStartUp;

                switch (G_CommonClassXML.G_XML_Read("", G_CommonDefine.CommonFile.CommonDefine, Nodes, out getStartUpInfo))
                {
                    case G_CommonDefine.RetValue.FoldarNotExistsError:
                    case G_CommonDefine.RetValue.FileNotExistsError:
                    case G_CommonDefine.RetValue.UndefinedError:
                    case G_CommonDefine.RetValue.NoData:
                        return false;
                    case G_CommonDefine.RetValue.GetData:
                        //正常処理
                        break;
                    default:
                        return false;
                }

                Nodes[0] = G_CommonDefine.NodeNM.Common.connectionNode;
                Nodes[1] = G_CommonDefine.NodeNM.Common.SubLog;

                switch (G_CommonClassXML.G_XML_Read("", G_CommonDefine.CommonFile.CommonDefine, Nodes, out getLogInfo))
                {
                    case G_CommonDefine.RetValue.FoldarNotExistsError:
                    case G_CommonDefine.RetValue.FileNotExistsError:
                    case G_CommonDefine.RetValue.UndefinedError:
                    case G_CommonDefine.RetValue.NoData:
                        return false;
                    case G_CommonDefine.RetValue.GetData:
                        //正常処理
                        break;
                    default:
                        return false;
                }

                Nodes[0] = G_CommonDefine.NodeNM.Common.connectionNode;
                Nodes[1] = G_CommonDefine.NodeNM.Common.SubColor;

                switch (G_CommonClassXML.G_XML_Read("", G_CommonDefine.CommonFile.CommonDefine, Nodes, out getColorInfo))
                {
                    case G_CommonDefine.RetValue.FoldarNotExistsError:
                    case G_CommonDefine.RetValue.FileNotExistsError:
                    case G_CommonDefine.RetValue.UndefinedError:
                    case G_CommonDefine.RetValue.NoData:
                        return false;
                    case G_CommonDefine.RetValue.GetData:
                        //正常処理
                        break;
                    default:
                        return false;
                }

                Nodes[0] = G_CommonDefine.NodeNM.Common.connectionNode;
                Nodes[1] = G_CommonDefine.NodeNM.Common.SubToolTip;

                switch (G_CommonClassXML.G_XML_Read("", G_CommonDefine.CommonFile.CommonDefine, Nodes, out getToolTipInfo))
                {
                    case G_CommonDefine.RetValue.FoldarNotExistsError:
                    case G_CommonDefine.RetValue.FileNotExistsError:
                    case G_CommonDefine.RetValue.UndefinedError:
                    case G_CommonDefine.RetValue.NoData:
                        return false;
                    case G_CommonDefine.RetValue.GetData:
                        //正常処理
                        break;
                    default:
                        return false;
                }

                Nodes[0] = G_CommonDefine.NodeNM.Common.connectionNode;
                Nodes[1] = G_CommonDefine.NodeNM.Common.SubInterval;

                switch (G_CommonClassXML.G_XML_Read("", G_CommonDefine.CommonFile.CommonDefine, Nodes, out getIntervalInfo))
                {
                    case G_CommonDefine.RetValue.FoldarNotExistsError:
                    case G_CommonDefine.RetValue.FileNotExistsError:
                    case G_CommonDefine.RetValue.UndefinedError:
                    case G_CommonDefine.RetValue.NoData:
                        return false;
                    case G_CommonDefine.RetValue.GetData:
                        //正常処理
                        break;
                    default:
                        return false;
                }

                /* CommonDefine.xml
                 * <DB>
                 * 0	:	DBServerIP
                 * 1	:	DBServer
                 * 2	:	DBCatalog
                 * 3	:	DBSecurity
                 * 4	:	DBUser
                 * 5	:	DBPass
                 * 6    :   DBBackUp
                 * 7    :   DBLogFolder
                 * 8    :   DBLogLevel
                 * <System>                 
                 * 0    :   exeName
                 * 1    :   exetempName
                 * 2    :   exePathLocal 
                 * 3    :   exePathServer
                 * 4    :   WaitFormFG
                 * 5    :   VersionFG
                 * 6    :   Version                 
                 * 7    :   SystemName
                 * 8    :   SystemTitle
                 * 9    :   SystemLicense
                 * <Encrypt>
                 * 0    :   EncryptKey
                 * 1    :   EncryptUser
                 * 2    :   EncryptPass
                 * <EDI>
                 * 0    :   EDIFolder
                 * 1    :   EDIFolderPlan
                 * 2    :   EDIFolderResult 
                 * 3    :   EDIFolderSyk
                 * 4    :   EDIFolderProduct
                 * 5    :   EDIFolderFoward 
                 * 6    :   EDIFolderLoStock
                 * <FMT>
                 * 0    :   FMTFolder
                 * 1    :   FMTFolderFoward
                 * <StartUp>
                 * 0    :   StartUpLink
                 * 1    :   StartUpFolder
                 * <Log>
                 * 0    :   LogFolder
                 * 1    :   LogFile
                 * 2    :   LogSize
                 * <Color>
                 * 0    :   ColorFocusEnter
                 * 1    :   COlorFocusLeave
                 * 2    :   ColorReadOnly
                 * 3    :   ColorMRowPattern1
                 * 4    :   ColorMRowPattern2
                 * <ToolTip>
                 * 0    :   ToolTipInitialDelay
                 * 1    :   ToolTipReshowDelay
                 * 2    :   ToolTipAutoPopDelay
                 * 3    :   ToolTipShowAlwaysFG
                 * <Interval>
                 * 0    :   IntervalLogIn
                 * 1    :   IntervalMenu
                 * 2    :   IntervalReport
                 */

                G_CommonDefine.G_DBServerIP = getDBInfo[0, 0].NodeValue.ToString();
                G_CommonDefine.G_DBServer = getDBInfo[0, 1].NodeValue.ToString();
                G_CommonDefine.G_DBCatalog = getDBInfo[0, 2].NodeValue.ToString();
                G_CommonDefine.G_DBSecurity = getDBInfo[0, 3].NodeValue.ToString();
                G_CommonDefine.G_DBUser = getDBInfo[0, 4].NodeValue.ToString();
                G_CommonDefine.G_DBPass = getDBInfo[0, 5].NodeValue.ToString();
                G_CommonDefine.G_DBBackUp = getDBInfo[0, 6].NodeValue.ToString();
                G_CommonDefine.G_DBLogFolder = getDBInfo[0, 7].NodeValue.ToString();
                G_CommonDefine.G_DBLogLevel = Int32.Parse(getDBInfo[0, 8].NodeValue.ToString());

                G_CommonDefine.G_SystemPC_exeName = getSystemPCInfo[0, 0].NodeValue.ToString();
                G_CommonDefine.G_SystemPC_exetempName = getSystemPCInfo[0, 1].NodeValue.ToString();
                G_CommonDefine.G_SystemPC_exePathLocal = getSystemPCInfo[0, 2].NodeValue.ToString();
                G_CommonDefine.G_SystemPC_exePathServer = getSystemPCInfo[0, 3].NodeValue.ToString();
                if (getSystemPCInfo[0, 4].NodeValue.ToString() == "ON")
                {
                    G_CommonDefine.G_SystemPC_WaitFormFG = true;
                }
                else
                {
                    G_CommonDefine.G_SystemPC_WaitFormFG = false;
                }

                G_CommonDefine.G_SystemPC_VersionFG = getSystemPCInfo[0, 5].NodeValue.ToString();
                G_CommonDefine.G_SystemPC_Version = getSystemPCInfo[0, 6].NodeValue.ToString();
                G_CommonDefine.G_SystemPC_SystemName = getSystemPCInfo[0, 7].NodeValue.ToString();
                G_CommonDefine.G_SystemPC_SystemTitle = getSystemPCInfo[0, 8].NodeValue.ToString();
                G_CommonDefine.G_SystemPC_SystemLicense = getSystemPCInfo[0, 9].NodeValue.ToString();

                G_CommonDefine.G_EncryptKey = getEncryptInfo[0, 0].NodeValue.ToString();
                G_CommonDefine.G_EncryptUser = getEncryptInfo[0, 1].NodeValue.ToString();
                G_CommonDefine.G_EncryptPass = getEncryptInfo[0, 2].NodeValue.ToString();

                G_CommonDefine.G_EDIFolder = getEDIInfo[0, 0].NodeValue.ToString();
                G_CommonDefine.G_EDIFolderPlan = getEDIInfo[0, 1].NodeValue.ToString();
                G_CommonDefine.G_EDIFolderResult = getEDIInfo[0, 2].NodeValue.ToString();
                G_CommonDefine.G_EDIFolderSyk = getEDIInfo[0, 3].NodeValue.ToString();
                G_CommonDefine.G_EDIFolderNyk = getEDIInfo[0, 4].NodeValue.ToString();
                G_CommonDefine.G_EDIFolderFormat = getEDIInfo[0, 5].NodeValue.ToString();
                G_CommonDefine.G_EDIFolderProduct = getEDIInfo[0, 6].NodeValue.ToString();
                G_CommonDefine.G_EDIFolderFoward = getEDIInfo[0, 7].NodeValue.ToString();
                G_CommonDefine.G_EDIFolderLoStock = getEDIInfo[0, 8].NodeValue.ToString();
                G_CommonDefine.G_EDIFolderInitialDir = getEDIInfo[0, 9].NodeValue.ToString();
                G_CommonDefine.G_EDIInitialFile = getEDIInfo[0,10].NodeValue.ToString();
                G_CommonDefine.G_EDIFilter = getEDIInfo[0, 11].NodeValue.ToString();
                G_CommonDefine.G_EDIFolderSend = getEDIInfo[0, 12].NodeValue.ToString();

                G_CommonDefine.G_FMTFolder = getFMTInfo[0, 0].NodeValue.ToString();
                G_CommonDefine.G_FMTFolderFoward = getFMTInfo[0, 1].NodeValue.ToString();

                G_CommonDefine.G_StartUpLink = getStartUpInfo[0, 0].NodeValue.ToString();
                G_CommonDefine.G_StartUpFolder = getStartUpInfo[0, 1].NodeValue.ToString();

                G_CommonDefine.G_LogFolder = getLogInfo[0, 0].NodeValue.ToString();
                G_CommonDefine.G_LogFile = getLogInfo[0, 1].NodeValue.ToString();
                G_CommonDefine.G_LogSize = Int32.Parse(getLogInfo[0, 2].NodeValue.ToString());

                G_CommonDefine.G_ColorFocusEnter = getColorInfo[0, 0].NodeValue.ToString();
                G_CommonDefine.G_ColorFocusLeave = getColorInfo[0, 1].NodeValue.ToString();
                G_CommonDefine.G_ColorReadOnly = getColorInfo[0, 2].NodeValue.ToString();
                G_CommonDefine.G_ColorMRowPattern1 = getColorInfo[0, 3].NodeValue.ToString();
                G_CommonDefine.G_ColorMRowPattern2 = getColorInfo[0, 4].NodeValue.ToString();

                G_CommonDefine.G_ToolTipInitialDelay = Int32.Parse(getToolTipInfo[0, 0].NodeValue.ToString());
                G_CommonDefine.G_ToolTipReshowDelay = Int32.Parse(getToolTipInfo[0, 1].NodeValue.ToString());
                G_CommonDefine.G_ToolTipAutoPopDelay = Int32.Parse(getToolTipInfo[0, 2].NodeValue.ToString());
                if (getToolTipInfo[0, 3].NodeValue.ToString() == "ON")
                {
                    G_CommonDefine.G_ToolTipShowAlwaysFG = true;
                }
                else
                {
                    G_CommonDefine.G_ToolTipShowAlwaysFG = false;
                }

                G_CommonDefine.G_IntervalLogIn = Int32.Parse(getIntervalInfo[0, 0].NodeValue.ToString());
                G_CommonDefine.G_IntervalMenu = Int32.Parse(getIntervalInfo[0, 1].NodeValue.ToString());
                G_CommonDefine.G_IntervalReport = Int32.Parse(getIntervalInfo[0, 2].NodeValue.ToString());

                //パスワードを複合化
                G_CommonDefine.G_DBPass = G_CommonClassEncrypt.G_Encrypt_DecryptString(G_CommonDefine.G_DBPass, G_CommonDefine.G_EncryptKey);
                G_CommonDefine.G_EncryptPass = G_CommonClassEncrypt.G_Encrypt_DecryptString(G_CommonDefine.G_EncryptPass, G_CommonDefine.G_EncryptKey);

                //MultiRowSheetの交互色設定
                G_CommonDefine.G_col_MultiRowSheetBackColor = new Color[] { ColorTranslator.FromHtml(G_CommonDefine.G_ColorMRowPattern1), ColorTranslator.FromHtml(G_CommonDefine.G_ColorMRowPattern2) };

                //ログ書込み
                //G_CommonClassSystem.G_System_SetLog(G_CommonDefine.LogMode.ProcEnd, "P_GET_CommonDefine処理終了", "P_GET_CommonDefine");

                return true;
            }
            catch (Exception err)
            {
                //ログ書込み
                //G_CommonClassSystem.G_System_SetLog(G_CommonDefine.LogMode.Error, "P_GET_CommonDefine処理エラー" + err.Message, "P_GET_CommonDefine");

                //エラーメッセージ表示
                MessageBox.Show(err.Message, Resources.エラー, MessageBoxButtons.OK, MessageBoxIcon.Error);

                return false;
            }
        }

        ////////////////////////////////////////////////////////////////////////////
        // OutLine   : G_System_GetRelationFiles
        // Parameter : なし
        // Return    : True Or False
        // Contents  : 関連ファイルの取得
        // Makes     : 荒木
        // History   : 2008/03/03       : 初版
        //           :
        ////////////////////////////////////////////////////////////////////////////
        public static bool G_System_GetRelationFiles()
        {
            try
            {
                string str_LocalZip = G_CommonDefine.G_CommonPath + "include.zip";

                //関連ファイルの存在を確認して無ければダウンロード
                if (File.Exists(str_LocalZip))
                {
                    return true;
                }

                MessageBox.Show(Resources.関連ファイルをタウンロートします, Resources.確認, MessageBoxButtons.OK, MessageBoxIcon.Information);

                //待機画面表示
                G_CommonClassOther.G_Other_WaitForm(true);

                string str_ServerZip = G_CommonDefine.G_SystemPC_exePathServer.ToString() + "include.zip";

                //include.zipをサーバーからローカルにコピー
                File.Copy(str_ServerZip, str_LocalZip, true);

                //include.zipを解凍
                G_CommonClassCompress.G_Compress_DeCompression(str_LocalZip, G_CommonDefine.G_CommonPath);

                string str_LocalInclude = G_CommonDefine.G_CommonPath + "include";

                //includeフォルダ内の全ファイルをexeのあるフォルダ内にコピー
                foreach (string str_CopyFrom in Directory.GetFiles(str_LocalInclude))
                {
                    string str_CopyTo = Path.Combine(G_CommonDefine.G_CommonPath, Path.GetFileName(str_CopyFrom));

                    File.Copy(str_CopyFrom, str_CopyTo, true);

                    //コピーが終了したら削除
                    File.Delete(str_CopyFrom);
                }

                //不要になったincludeフォルダを削除
                Directory.Delete(str_LocalInclude);

                //待機画面解除
                G_CommonClassOther.G_Other_WaitForm(false);

                return true;
            }
            catch (Exception err)
            {
                MessageBox.Show(err.Message, "エラー", MessageBoxButtons.OK, MessageBoxIcon.Error);

                return false;
            }
        }

        ////////////////////////////////////////////////////////////////////////////
        // OutLine   : G_System_GetCommonPath
        // Parameter : なし
        // Return    : EXEファイルの格納されているPath
        // Contents  : EXEファイルの格納されているPathを取得
        // Makes     : 渡辺
        // History   : 2007/06/14       : 初版
        //           :
        ////////////////////////////////////////////////////////////////////////////
        public static string G_System_GetCommonPath()
        {
            //using System.Reflection;  //左記をusingして下さい。(HHT用)
            return Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location).ToString() + "\\";
        }

        ////////////////////////////////////////////////////////////////////////////
        // OutLine   : G_System_GetDeskTopPath
        // Parameter : なし
        // Return    : 現在ログインしているユーザーのデスクトップのPath
        // Contents  : 現在ログインしているユーザーのデスクトップのPathを取得
        // Makes     : 渡辺
        // History   : 2007/06/14       : 初版
        //           :
        ////////////////////////////////////////////////////////////////////////////
        public static string G_System_GetDeskTopPath()
        {
            return Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
        }

        ////////////////////////////////////////////////////////////////////////////
        // OutLine   : G_System_GetHostName
        // Parameter : なし
        // Return    : PC名称
        // Contents  : PC名称取得関数
        // Makes     : 渡辺
        // History   : 2007/06/14       : 初版
        //           :
        ////////////////////////////////////////////////////////////////////////////
        public static string G_System_GetHostName()
        {
            try
            {
                //開始時にログ作成
                //                G_System_SetLog(G_CommonDefine.LogMode.NetProcStart, "PC名取得開始", "G_System_GetHostName");

                string str_PCNM = "";

                str_PCNM = Dns.GetHostName();

                //終了時にログ作成
                //                G_System_SetLog(G_CommonDefine.LogMode.NetProcEnd, "PC名取得終了", "G_System_GetHostName");

                return str_PCNM;
            }
            catch (Exception err)
            {
                MessageBox.Show(Resources.PC名称の取得に失敗しました + err.Message, Resources.エラー);

                //エラー発生時ログ書き込み
                //G_System_SetLog(G_CommonDefine.LogMode.Error, "PC名取得失敗", "G_System_GetHostName");

                return "";
            }
        }

        public static string G_System_GetIPAddress()
        {
            IPAddress addr;
            // 获得本机局域网IP地址
            addr = new IPAddress(Dns.GetHostByName(Dns.GetHostName()).AddressList[0].Address);
            return addr.ToString();
        }

        ////////////////////////////////////////////////////////////////////////////
        // OutLine   : G_System_SetLog
        // Parameter : mode         :int        :出力モード(デバッグ・エラー)
        //           : logMessage   :string     :ログに書き込む内容
        //           : procName     :string     :呼び出し元関数名
        //           : なし
        // Return    : なし
        // Contents  : ログ書き込み関数
        // Makes     : 渡辺
        // History   : 2007/06/14       : 初版
        //           :
        ////////////////////////////////////////////////////////////////////////////
        public static void G_System_SetLog(int mode, string logMessage, string procName)
        {
            try
            {
                //Logフォルダ存在確認
                if (!Directory.Exists(G_CommonDefine.G_CommonPath + G_CommonDefine.G_LogFolder))
                {
                    //Logフォルダがないときは作成する
                    Directory.CreateDirectory(G_CommonDefine.G_CommonPath + G_CommonDefine.G_LogFolder);
                }

                //Logファイル存在確認
                if (!File.Exists(G_CommonDefine.G_CommonPath + G_CommonDefine.G_LogFolder + G_CommonDefine.G_LogFile))
                {
                    //ログファイルがないとき、ファイルを作成する
                    FileStream createFile = File.Create(G_CommonDefine.G_CommonPath + G_CommonDefine.G_LogFolder + G_CommonDefine.G_LogFile, G_CommonDefine.G_LogSize);
                    createFile.Close();
                }

                string str_ModeNM = "";

                switch (mode)
                {
                    case G_CommonDefine.LogMode.Other:
                        str_ModeNM = Resources.その他;
                        break;
                    case G_CommonDefine.LogMode.Error:
                        str_ModeNM = Resources.エラー;
                        break;
                    case G_CommonDefine.LogMode.Debug:
                        str_ModeNM = Resources.テハック;
                        break;
                    case G_CommonDefine.LogMode.ProcStart:
                        str_ModeNM = Resources.処理開始;
                        break;
                    case G_CommonDefine.LogMode.ProcEnd:
                        str_ModeNM = Resources.処理終了;
                        break;
                    case G_CommonDefine.LogMode.NetProcStart:
                        str_ModeNM = Resources.ネットワーク開始;
                        break;
                    case G_CommonDefine.LogMode.NetProcEnd:
                        str_ModeNM = Resources.ネットワーク終了;
                        break;
                    default:
                        str_ModeNM = Resources.不明;
                        break;
                }

                StreamWriter logFile = new StreamWriter(G_CommonDefine.G_CommonPath + G_CommonDefine.G_LogFolder + G_CommonDefine.G_LogFile, true, Encoding.UTF8);

                //ログファイルの情報を取得し、ファイルサイズを確認する
                FileInfo info = new FileInfo(G_CommonDefine.G_CommonPath + G_CommonDefine.G_LogFolder + G_CommonDefine.G_LogFile);
                if ((int)info.Length > G_CommonDefine.G_LogSize)
                {
                    logFile.Close();

                    File.Copy(G_CommonDefine.G_CommonPath + G_CommonDefine.G_LogFolder + G_CommonDefine.G_LogFile, G_CommonDefine.G_CommonPath + G_CommonDefine.G_LogFolder + DateTime.Now.ToString("yyyyMMddHHmmss") + G_CommonDefine.G_LogFile);

                    File.Create(G_CommonDefine.G_CommonPath + G_CommonDefine.G_LogFolder + G_CommonDefine.G_LogFile, G_CommonDefine.G_LogSize).Close();

                    logFile = new StreamWriter(G_CommonDefine.G_CommonPath + G_CommonDefine.G_LogFolder + G_CommonDefine.G_LogFile, true, Encoding.UTF8);
                }

                //ログファイルに書き込み
                logFile.Write(DateTime.Now.ToString() + str_ModeNM + " : " + logMessage + "(" + procName + ")\n");

                logFile.Close();
            }
            catch (Exception err)
            {
                MessageBox.Show(Resources.G_System_SetLog + err.Message, Resources.エラー);
            }
        }

        ////////////////////////////////////////////////////////////////////////////
        // OutLine   : G_System_GetMessage
        // Parameter : keyName      :string     :XML File Item
        //           :    
        //           :           
        //           : 
        // Return    : string
        // Contents  : Messagebox prompt information
        // Makes     : michael
        // History   : 2008/10/30       : michael
        //           :
        ////////////////////////////////////////////////////////////////////////////
        public static string G_System_GetMessage(string keyName)
        {
            string strDescription = string.Empty;
            string languageName = Thread.CurrentThread.CurrentUICulture.ToString();

            string path = Path.GetDirectoryName(Application.ExecutablePath);
            string filename = string.Empty;
            switch (languageName)
            {
                case "zh-CHS":
                    filename = path + @"\" + G_CommonDefine.CommonMessageCN;
                    break;
                case "en":
                default:
                    filename = path + @"\" + G_CommonDefine.CommonMessageEN;
                    break;
            }

            if (!File.Exists(filename))
            {
                G_System_SetLog(G_CommonDefine.LogMode.Error, filename + " not find.", "G_CommonClassSystem.G_System_GetMessage");
                return strDescription;
            }


            XmlDocument doc = new XmlDocument();

            doc.Load(filename);

            XmlNode node = doc.SelectSingleNode("//" + languageName + "/cd[@KeyName='" + keyName + "']/Description");

            if (node != null)
            {

                strDescription = node.InnerText;
            }
            else
            {
                G_System_SetLog(G_CommonDefine.LogMode.Error, "not find  message of " + "/" + languageName + "/" + keyName, "G_CommonClassSystem.G_System_GetMessage");
            }
            return strDescription;
        }
        //创建目录
        public static void CreateDirtory(string path,bool IsNetworkPath)
        {
            if (!Directory.Exists(path))
            {
                if (IsNetworkPath)
                    path = path.Substring(2);
                string[] dirArray = path.Split('\\');
                string temp = string.Empty;
                if (IsNetworkPath)
                    temp = @"\\";
                for (int i = 0; i < dirArray.Length; i++)
                {
                    temp += dirArray[i].Trim() + "\\";
                    if (IsNetworkPath)
                    {
                        if (i > 0)
                            if (!Directory.Exists(temp))
                                Directory.CreateDirectory(temp);
                    }
                    else
                    {
                        if (!Directory.Exists(temp))
                            Directory.CreateDirectory(temp);
                    }
                        
                }
            }
        }

        //复制文件;
        public static void CopyFile(string sourcePath, string objPath)
        {
            //			char[] split = @"\".ToCharArray();
            if (!Directory.Exists(objPath))
            {
                Directory.CreateDirectory(objPath);
            }
            string[] files = Directory.GetFiles(sourcePath);
            for (int i = 0; i < files.Length; i++)
            {
                string[] childfile = files[i].Split('\\');
                File.Copy(files[i], objPath + @"\" + childfile[childfile.Length - 1], true);
            }
            string[] dirs = Directory.GetDirectories(sourcePath);
            for (int i = 0; i < dirs.Length; i++)
            {
                string[] childdir = dirs[i].Split('\\');
                CopyFile(dirs[i], objPath + @"\" + childdir[childdir.Length - 1]);
            }
        }

        public static void SaveDataToCVS(DataTable dt, string fileName, bool IsDoubleQuotation)
        {
            using (StreamWriter sw = new StreamWriter(fileName, false, Encoding.Default))
            {
                string line;
                foreach (DataRow dr in dt.Rows)
                {
                    line = GenerateLine(dr, IsDoubleQuotation);
                    sw.WriteLine(line);
                }
                sw.Close();
            }
        }

        private static string GenerateLine(DataRow dr, bool IsDoubleQuotation)
        {
            string retLine = string.Empty;

            string field = string.Empty;

            foreach (DataColumn dc in dr.Table.Columns)
            {
                field = dr[dc.ColumnName].ToString();

                if (IsDoubleQuotation)
                {
                    field = "\"" + field + "\"";
                }
                else
                {
                    if (field.IndexOf('"') >= 0)
                    {
                        field = field.Replace("\"", "\"\"");
                    }

                    if (field.IndexOf(',') >= 0 || field.IndexOf('"') >= 0)
                    {
                        field = "\"" + field + "\"";
                    }
                }
                retLine += field + ",";

                field = string.Empty;
            }

            return retLine;
        }

    }//G_CommonClassSystem end


    ////////////////////////////////////////////////////////////////////////////
    // OutLine   : DetectPrevInstance
    // Parameter : mode         :int        :出力モード(デバッグ・エラー)
    //           : logMessage   :string     :ログに書き込む内容
    //           : procName     :string     :呼び出し元関数名
    //           : なし
    // Return    : なし
    // Contents  : ２重起動用
    // Makes     : 渡辺
    // History   : 2007/06/14       : 初版
    //           :
    ////////////////////////////////////////////////////////////////////////////
    internal class DetectPrevInstance
    {
        [DllImport("CoreDll")]
        public static extern int FindWindow(char[] className, char[] WindowsName);

        [DllImport("CoreDll")]
        public static extern bool SetForegroundWindow(int hWnd);

        public bool IsRunning(string WindowName)
        {
            int hWnd;
            hWnd = FindWindow(null, WindowName.ToCharArray());
            if (hWnd != 0)
            {
                SetForegroundWindow(hWnd);
                return true;
            }
            else
            {
                return false;
            }
        }
    }//DetectPrevInstance end
}//namespace end
//--------------------------------按高手的意思修改BackgroundWorker，如下

//使用BackgroundWorker异步进度条



namespace FBWMS

{

    class MyNewProgress

    {

        private BackgroundWorker backgroundWorker = null;

        //by 闫磊Email:Landgis@126.com,yanleigis@21cn.com 2007.10.31

        private int MaxNum;

        Form progressForm = null;

        ProgressBar progressBar1 = null;

        

        Label label1;

        

        private void btn_Click(object sender, EventArgs e)

        {

            if (backgroundWorker.IsBusy)

            {

                if (MessageBox.Show("你确定终止吗", "终止", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)

                backgroundWorker.CancelAsync();

            }

        }

        public void OnProcessCompleted(object sender, EventArgs e)

        {

            Dispose();

        }

        

        public void OnProgressChanged(object sender, ProgressChangedEventArgs e)

        {

            progressBar1.Value = e.ProgressPercentage;

            label1.Text = "目前完成:" + (progressBar1.Value * 100 / progressBar1.Maximum) + "%";

        }

        private void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)

        {

            //return;

            

            /**//*if (e.Cancelled)

            {

                MessageBox.Show("取消");

            }

            else if (e.Error != null)

            {

                MessageBox.Show("错误");

            }

            else

            {

                

                MessageBox.Show("完成");

            }

            */

        }

        private void Form_FormClosing(object sender, FormClosingEventArgs e) //处理窗口直接关闭事件

        {

            if (backgroundWorker.IsBusy)

            {

                if (MessageBox.Show("你确定终止吗", "终止", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)

                backgroundWorker.CancelAsync();

                else

                e.Cancel = true;

            }

        }

        

        //把事件传进入

        private void SetbackgroundWorker(DoWorkEventHandler DoWork, ProgressChangedEventHandler worker_ProgressChanged)

        {

            backgroundWorker = new BackgroundWorker();

            backgroundWorker.WorkerReportsProgress = true;//有进度条

            backgroundWorker.WorkerSupportsCancellation = true;//是否支持异步取消

            backgroundWorker.DoWork += new DoWorkEventHandler(DoWork);

            backgroundWorker.ProgressChanged += new ProgressChangedEventHandler(worker_ProgressChanged);//做的事情

            backgroundWorker.ProgressChanged += new ProgressChangedEventHandler(OnProgressChanged);//更新进度条

            backgroundWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(worker_RunWorkerCompleted);//完成事件

            backgroundWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(OnProcessCompleted);

            backgroundWorker.RunWorkerAsync();

        }

        public MyNewProgress(int Max, String Caption, bool IsCancel, DoWorkEventHandler DoWork, ProgressChangedEventHandler worker_ProgressChanged)//最大值和标题

        {

            progressForm = new Form();

            progressForm.MinimizeBox = false;

            progressForm.MaximizeBox = false;

            progressForm.StartPosition = FormStartPosition.CenterScreen;

            progressForm.Width = 326 + 19;

            progressForm.Height = 96 + 19 + 20;

            progressForm.Text = Caption;

            progressForm.TopMost = true;//设置窗口在上边

            label1 = new Label();

            label1.Left = 9;

            label1.Top = 15;

            label1.Parent = progressForm;

            progressBar1 = new ProgressBar();

            progressBar1.Maximum = Max;

            MaxNum = Max;

            progressBar1.Left = 9;

            progressBar1.Top = 25 + 15;

            progressBar1.Width = 310;

            progressBar1.Parent = progressForm;

            progressBar1.Value = 0;

            

            if (IsCancel)

            {

                Button btnCancel = new Button();

                btnCancel.Text = "取消";

                btnCancel.Left = 240;

                btnCancel.Top = 54 + 20;

                btnCancel.Parent = progressForm;

                btnCancel.Click += new System.EventHandler(this.btn_Click);

                

            }

            progressForm.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.Form_FormClosing);

            progressForm.Show();

            SetbackgroundWorker(DoWork, worker_ProgressChanged);

            

        }

        public void Dispose()

        {

            if (progressForm != null)

            {

                progressBar1.Dispose();

                progressForm.Dispose();

            }

        }

    }

}
/*
//测试方法，放一个按钮button和标签label1

private void worker_DoWork(object sender, DoWorkEventArgs e)

{

    DoNothing((BackgroundWorker)sender, e);

}

 

private void DoNothing(BackgroundWorker worker, DoWorkEventArgs e)

{

    //要做的事情，写在这里

    int num = 100;

    for (int i = 0; i <num; i++)

    {

        // Check for cancellation

        if (worker.CancellationPending)

        {

            e.Cancel = true;

            break;

        }

        else

        {

            worker.ReportProgress((i + 1) * (100 / num));

            

            Thread.Sleep(100);

        }

    }

    

}

 

private void worker_ProgressChanged(object sender, ProgressChangedEventArgs e)

{

    label1.Text = e.ProgressPercentage.ToString();

    

}

private void button1_Click(object sender, EventArgs e)

{

    MyNewProgress myProgress = new MyNewProgress(100, "进度条", true, worker_DoWork, worker_ProgressChanged);

    

}
*/
